package com.jye.rseven.user.util;

import com.jye.rseven.Constants.MyConstants;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class FileUpload {
    public static String uploadImage(MultipartFile multipartFile){

        String url = MyConstants.tracker_server;
        String track = FileUpload.class.getResource("/tracker.conf").getPath();

        String trackerServers = "39.106.100.179:22122";

        try {
            //ClientGlobal.init(track);
            ClientGlobal.initByTrackers(trackerServers);
        } catch (Exception e) {
            e.printStackTrace();
        }

        TrackerClient trackerClient=new TrackerClient();
        TrackerServer trackerServer= null;

        try {
            trackerServer = trackerClient.getTrackerServer();
        } catch (Exception e) {
            e.printStackTrace();
        }
        StorageClient storageClient=new StorageClient(trackerServer,null);
        //String orginalFilename="E:/git/GitClone/r6_30/r7_test/src/static/icon/icon1.png";

        try {
            //二进制
            byte[] bytes = multipartFile.getBytes();
            //后缀名
            String originalFilename = multipartFile.getOriginalFilename();
            //获取.
            int i = originalFilename.lastIndexOf(".");
            //截
            String extName = originalFilename.substring(i + 1);
            String[] upload_file = storageClient.upload_file(bytes, extName, null);
            for (String s : upload_file) {
                url +="/"+ s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }
}
